
//
//  String + Regular.swift
//  04正则表达式（来自xx平台）
//
//  Created by Bingo on 15/11/11.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import Foundation

extension String{

    
    func linkSource() ->String{
     // 匹配的规则
        let pattern = ">(.*?)</a>"
        
        let regular = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
        
        let result = regular.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: self.characters.count))
        
        let count = result?.numberOfRanges ?? 0
        
        if count > 1{
        
            let range = result!.rangeAtIndex(1)
            
            let text = (self as NSString).substringWithRange(range)
            
            return text
        }else{
        
            return "未知来源"
        }
    }
    
}